namespace Table {
  export interface Pageable {
    pageNum: number;
    pageSize: number;
    total: number;
  }
  export interface StateProps {
    tableData: any[];
    pageable: Pageable;
    searchParam: {
      [key: string]: any;
    };
    searchInitParam: {
      [key: string]: any;
    };
    totalParam: {
      [key: string]: any;
    };
    icon?: {
      [key: string]: any;
    };
  }
}

/**
 * @description table 页面操作方法封装
 * @param {Function} api 获取表格数据 api 方法 (必传)
 * @param {Object} initParam 获取数据初始化参数 (非必传，默认为{})
 * @param {Boolean} isPageable 是否有分页 (非必传，默认为true)
 * @param {Function} dataCallBack 对后台返回的数据进行处理的方法 (非必传)
 * @param {Function} requestError 请求错误时处理 (非必传)
 * */
export const useTable = (
  api?: (params: any) => Promise<any>,
  initParam: object = {},
  isPageable: boolean = true,
  dataCallBack?: (data: any) => any,
  requestError?: (error: any) => void
) => {
  const state = reactive<Table.StateProps>({
    // 表格数据
    tableData: [],
    // 分页数据
    pageable: {
      // 当前页数
      pageNum: 1,
      // 每页显示条数
      pageSize: 10,
      // 总条数
      total: 0,
    },
    // 查询参数(只包括查询)
    searchParam: {},
    // 初始化默认的查询参数
    searchInitParam: {},
    // 总参数(包含分页和查询参数)
    totalParam: {},
  });

  /**
   * @description 分页查询参数(只包括分页和表格字段排序,其他排序方式可自行配置)
   * */
  const pageParam = computed({
    get: () => {
      return {
        pageNum: state.pageable.pageNum,
        pageSize: state.pageable.pageSize,
      };
    },
    set: (newVal: any) => {
      console.log("我是分页更新之后的值", newVal);
    },
  });

  //   获取表格数据
  const getTableList = async () => {
    if (!api) return;
    console.log("获取表格数据");

    Object.assign(
      state.totalParam,
      initParam,
      isPageable ? pageParam.value : {}
    );
    let { data } = await api({ ...state.searchInitParam, ...state.totalParam });
    console.log(86, data);
    dataCallBack && (data = dataCallBack(data));
    state.tableData = isPageable ? data.list : data;
    if (isPageable) {
      const { pageNum, pageSize, total } = data;
      updatePageable({ pageNum, pageSize, total });
    }
  };

  //   更新分页信息
  const updatePageable = (pageable: Table.Pageable) => {
    console.log("更新分页信息", pageable);
    Object.assign(state.pageable, pageable);
  };

  // 表格数据查询
  const search = () => {
    console.log("表格数据查询");
    state.pageable.pageNum = 1;
    updatedTotalParam();
    getTableList();
  };

  // 表格数据重置
  const reset = () => {
    console.log("表格数据重置");
  };
  // 每页条数改变
  const handleSizeChange = (val: number) => {
    console.log("每页条数改变",val);
    state.pageable.pageNum = 1;
    state.pageable.pageSize = val;
    getTableList();
  };
  // 当前页改变
  const handleCurrentChange = (val: number) => {
    console.log("当前页改变", val);
    state.pageable.pageNum = val;
    getTableList();
  };

  // 更新查询参数
  const updatedTotalParam = () => {
    console.log("更新查询参数");
  };

  return {
    ...toRefs(state),
    getTableList,
    search,
    reset,
    handleSizeChange,
    handleCurrentChange,
    updatedTotalParam,
  };
};
